
require('/utils/Page')

// app.js
const utils = require('/utils/util');
import formatTime,{formatNumber} from '/utils/util'
console.log(utils)
const request = require('/utils/request')
wx.$https = require('/utils/request');
console.log(utils)
let $https = require('/utils/request')
wx.$https = $https
wx.baseURL = $https.baseURL
wx.get = $https.get
wx.put = $https.put
wx.post = $https.post
wx.delete = $https.delete


App({
  onLaunch() {
    // 展示本地存储能力
    const logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)

    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        console.log('I am wx.login',res)
        wx.get('http://localhost:3000/wxlogin?code=' + res.code).then(res=>{
          // console.log(res)
          wx.userInfo = res
          wx.token = res.token
          if(wx.waitLoginPages.length){
            for(let i = 0; i< wx.waitLoginPages.length; i ++) {
              let page = wx.waitLoginPages[i]
              page.onLoad.call(page.context,page.options)
            }
          }
        })
      }
    })
  },
  globalData: {
    userInfo: null
  }
})

//定义两个方法，快速的按照学号查询该同学的姓名
// var student = [
//   {name:'张三',no:'201901'},
//   {name:'李四',no:'201902'},
//   {name:'王五',no:'201903'}
// ]
// // 方法一,for循环
// function findStudentByNo1(no){  
//   var studentName = []  
//   for( var i = 0;i < 3;i++)
//   {
//     if(student[i].no == no)
//     {
//       studentName[i] = student[i].name
//       return studentName[i]
//     }
//   }
// }
// console.log(findStudentByNo1('201903'))

// //方法二，数组转换成对象，转换成对象的时候就快速访问对象的值
// var obj = {}
// var obj1 = {}
// for (var i in student){
//   obj[student[i].name] = student[i].no //数组转化为键值对
// }
// function findStudentByNo2(no){
//   for(var i in obj) { //这里i索引
//     obj1[i] = obj[i]
//     if(obj[i] == no){
//      return i
//     }
//   }
// }
// console.log(findStudentByNo2('201902'))

// // 0-99随机数
// function random1(){
//   var random1 =  Math.random()*100
//   return random1
// }
// console.log(Math.ceil(random1()))

// // 99-999随机数
// function random2(){
//   var random2 =  (Math.random()*100) + 900
//   return random2
// }
// console.log(Math.ceil(random2()))


//日月年匹配
//作业一
function dateFormat(date,formatString){
  let year = date.getFullYear()//年
  formatString = formatString.replace(/(y+)/i,function($0){
    return year.toString().substr(4 - $0.length)
  })
  let month = date.getMonth() //系统月份是从零开始
  formatString = formatString.replace(/(m+)/i,function(){
    return month.toString() < 10 ? '0' + month.toString() : month.toString()
  })
  let day = date.getDate()//日
  formatString = formatString.replace(/(d+)/i,function(){
    return day.toString() < 10 ? '0' + day.toString() : day.toString()
  })
  let hour = date.getHours()//小时
  formatString = formatString.replace(/(h+)/i,function(){
    return hour.toString() < 10 ? '0' + hour.toString() : hour.toString()
  })
  let minute = date.getMinutes()//分钟
  formatString = formatString.replace(/(m+)/i,function(){
    return minute.toString() < 10 ? '0' + minute.toString() : minute.toString()
  })
  let second = date.getSeconds()//秒
  formatString = formatString.replace(/(s+)/i,function(){
    return second.toString() < 10 ? '0' + second.toString() : second.toString()
  })
  return formatString 
}
let date  = new Date()
date.setFullYear(2022)
date.setMonth(11)
date.setDate(8)
date.setHours(21)
date.setMinutes(5)
date.setSeconds(2)
//'YYYY-MM-DD hh:mm:ss'
console.log(dateFormat(date,'yyyy/mm/dd hh:mm:ss'))
// console.log(dateFormat(date,'yyyy年mm月'))


// 创建随机数组
//按照分页获取数组中的数据
//array数组
//pageNum开始页从1开始，默认1
//pageSize每页大小，默认10
//返回结果例子{total:10,row:[]} total为数据总数，rows为具体返回得数据

//[1,2,3,4,5,6,7,8] pageSize=5 pageNum=1
//{total:8,rows:[1,2,3,4,5]}
//作业二
function buildArray(){
  let array = []
  for(var i = 1;i < parseInt(Math.random()*100); i++) array.push(i)
  return array
}
function getList(array,pageNum,pageSize){
  let page = []
  //完成数据分页过程
  for(var i=(pageNum-1)*pageSize+1;i<=pageNum*pageSize;i++){
    page.push(i)
  }
  return '{'+'total:'+array.length+',rows:'+'['+page+']}'
}
let array = buildArray()
console.log(array)
console.log(getList(array,5,5)) 


//构造函数创建类
function Person(first, last, age, eye) {
  this.firstName = first
  this.lastName = last
  this.age = age
  this.eyeColor = eye
}
Person.prototype.name = 'jise'
// function() {
//   return this.firstName + " " + this.lastName
// }
var myFriend = new Person("Bill", "Gates", 62, "blue")
// console.log("My friend is " + myFriend.name())
console.log(myFriend.name)
